package com.qinglei.recoup.system.controller;

import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.common.exception.RecoupException;
import com.qinglei.recoup.system.pojo.AddMonitorTimeParam;
import com.qinglei.recoup.system.pojo.MonitorInfoTreeVO;
import com.qinglei.recoup.system.pojo.UpdateMonitorTimeParam;
import com.qinglei.recoup.system.pojo.UpdateOutBedTimeoutParam;
import com.qinglei.recoup.system.service.MonitorTimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * Created by qTo on 2021/1/17.
 */
@Api(tags = "监测仪管理相关控制器-张（二期）")
@Slf4j
@Validated
@RestController
@RequestMapping("monitorMgr")
public class MonitorManageController {

    @Resource
    private MonitorTimeService monitorTimeService;

    @ApiOperation(value = "查询检测仪工作配置 (楼层房间床) 树")
    @GetMapping
    public CommonRes<List<MonitorInfoTreeVO>> getMonitorInfoTree() {
        return CommonRes.ok(monitorTimeService.getMonitorInfoTree());
    }

    @ApiOperation(value = "添加检测仪工作时间段")
    @PostMapping("/workTime")
    public CommonRes addMonitorTime(@Valid @RequestBody AddMonitorTimeParam param) {
        try {
            monitorTimeService.addMonitorTime(param);
        } catch (RecoupException e) {
            return CommonRes.failed(e.getMessage());
        }
        catch (Exception e) {
            log.error("addMonitorTime error", e);
            return CommonRes.failed(e.getMessage());
        }
        return CommonRes.ok();
    }

    @ApiOperation(value = "修改检测仪工作时间段")
    @PutMapping("/workTime")
    public CommonRes updateMonitorTime(@Valid @RequestBody UpdateMonitorTimeParam param) {
        try {
            monitorTimeService.updateMonitorTime(param);
        } catch (RecoupException e) {
            return CommonRes.failed(e.getMessage());
        }
        catch (Exception e) {
            log.error("updateMonitorTime error", e);
            return CommonRes.failed(e.getMessage());
        }
        return CommonRes.ok();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型 1-床 2-房间 3-层 4-楼", paramType = "path", dataType = "int", required = true),
            @ApiImplicitParam(name = "monitorTimeId", value = "监控时间ID", paramType = "path", dataType = "int", required = true)
    })
    @ApiOperation(value = "删除检测仪工作时间段")
    @DeleteMapping("/workTime/{type}/{monitorTimeId}")
    public CommonRes delMonitorTime(@PathVariable @NotNull Integer type, @PathVariable @NotNull Integer monitorTimeId) {
        try {
            monitorTimeService.delMonitorTime(type, monitorTimeId);
        } catch (Exception e) {
            log.error("delMonitorTime error", e);
            return CommonRes.failed(e.getMessage());
        }
        return CommonRes.ok();
    }

    @ApiOperation(value = "修改离床时间阀值")
    @PutMapping("/outBedTimeout")
    public CommonRes updateOutBedTimeout(@Valid @RequestBody UpdateOutBedTimeoutParam param) {
        return monitorTimeService.updateOutBedTimeout(param);
    }

}
